" 
" Vilaris is a series of plugins for Vim to integrate it with dbx debugger. Vilaris is fast, 
" responsive and easy to setup. It works in console version of Vim. Most of
" shortcuts come from MS Visual Studio debugger which makes it easy for use by
" those who don't have expertise in Vim. Vim editor acts as a front-end for dbx. 
" Vim and dbx run in different terminal windows. The input such as next, step
" in, go to cursor, setting breakpoints is in vim window and output such as
" variable values, call stack or breakpoint status is in dbx.
"
"How to setup:
" 
"Plugins are fixed to work with any working directory. Let's say you work in project_name/OnBrown (you need to do this once per project)
" 
"in project/OnBrown
"$ mkfifo dbx_pipe
"$ mkfifo pipeup
" 
"in project_name
"$ ln -s OnBrown/dbx_pipe
"$ ln -s OnBrown/pipeup
" 
"You're ready to go. So while you create symbolic links to, the real pipes can be in whatever working path you like.
" 
" 
"How to start:
"[I recommend this order]
" 
"cat > dbx_pipe [only once per many dbx sessions]
" 
"vi --servername VISERV [from project root]
"dbx ../bin/TransmissionServer < dbx_pipe
" 
" 
"How to work:
"All your actions in VIM. The output is in dbx window. All necessary commands are shortcuts similar to Visual Studio Debugger. If you need to enter a command that has no shortcut just use :D <command> where <command> is just as it would appear in (dbx) prompt. I recommend using :D only for commands that don't change program counter location e.g. status, prints, dumps.
" 
" How to use command line arguments:
" How to insert input to program:
" 
" 
"Shortcuts:
"F5 - run(continue)
"Shift-F5 - quit debugger
"Ctrl-F5 - break execution
"F10 - next
"F11 - step
"Shift-F11 - step up
"Ctrl-F10 - go to cursor    [ULTRA]
"Double Click on Variable - print value [NEW - ULTRA]
"F12 on video selected text - print value
"F11 on video selected text - print 8 first bytes [Use on buffers]
"F9 - toggle breakpoint  [NEW - SUPER]
"Alt-F9 - status of breakpoints
"Ctrl-Shift-F9 - clear all breakpoints
"Alt-W - where (call stack)
"Shift-Up - go up the call stack
"Shift-Down - go down the call stack
"Alt-D - dump
"Alt-L - locate current execution line
"
" Test comment


map <silent> <unique> <F10> <Plug>NextDBX
nmap <silent> <script> <Plug>NextDBX :call NextDBX()<CR>
map <silent> <unique> <F11> <Plug>StepDBX
nmap <silent> <script> <Plug>StepDBX :call StepDBX()<CR>
map <silent> <unique> <S-F11> <Plug>StepUpDBX
nmap <silent> <script> <Plug>StepUpDBX :call StepUpDBX()<CR>
map <silent> <unique> <A-w> <Plug>WhereDBX
nmap <silent> <script> <Plug>WhereDBX :call WhereDBX()<CR>
map <silent> <unique> <A-F9> <Plug>StatusDBX
nmap <silent> <script> <Plug>StatusDBX :call StatusDBX()<CR>
map <silent> <unique> <S-Up> <Plug>MoveUpDBX
nmap <silent> <script> <Plug>MoveUpDBX :call MoveUpDBX()<CR>
map <silent> <unique> <S-Down> <Plug>MoveDownDBX
nmap <silent> <script> <Plug>MoveDownDBX :call MoveDownDBX()<CR>
map <silent> <unique> <F5> <Plug>RunDBX
nmap <silent> <script> <Plug>RunDBX :call RunDBX()<CR>
map <silent> <unique> <A-r> <Plug>RerunDBX
nmap <silent> <script> <Plug>RerunDBX :call RerunDBX()<CR>
map <silent> <unique> <S-F5> <Plug>StopDBX
nmap <silent> <script> <Plug>StopDBX :call StopDBX()<CR>
"map <silent> <unique> <2-LeftMouse> <Plug>PrintDBX
nmap <silent> <script> <Plug>PrintDBX :call PrintDBX()<CR>
map <silent> <unique> <C-F10> <Plug>GotoDBX
nmap <silent> <script> <Plug>GotoDBX :call GotoDBX()<CR>
map <silent> <unique> <C-S-F9> <Plug>ClearAllDBX
nmap <silent> <script> <Plug>ClearAllDBX :call ClearAllDBX()<CR>
map <silent> <unique> <C-F5> <Plug>BrkDBX
nmap <silent> <script> <Plug>BrkDBX :call BrkDBX()<CR>
map <silent> <unique> <A-l> <Plug>LocateDBX
nmap <silent> <script> <Plug>LocateDBX :call LocateDBX()<CR>
map <silent> <unique> <A-d> <Plug>DumpDBX
nmap <silent> <script> <Plug>DumpDBX :call DumpDBX()<CR>
map <silent> <unique> <A-n> <Plug>SetNextStatementDBX
nmap <silent> <script> <Plug>SetNextStatementDBX :call SetNextStatementDBX()<CR>
fun! NextDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	let cmd=["n"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! StepDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	let cmd=["s"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! StepUpDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["step up"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! WhereDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["where"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! StatusDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["status"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! MoveUpDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["up;sc"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! MoveDownDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["down;sc"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! RunDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	if exists("g:Vm_pc_sign_number")
		silent exe 'sign unplace ' . g:Vm_pc_sign_number
	endif
	echohl Error | echo 'No active DBX prompt.' | echohl None
	if !exists("g:dbx_running")
		let g:dbx_running = 0
	endif
	if g:dbx_running < 1
		let g:dbx_running = 1
		let cmd=["run"]
		map <silent> <unique> <2-LeftMouse> <Plug>PrintDBX
	else
		let cmd=["cont"]
	endif
	call writefile(cmd, "dbx_pipe")
endfun

fun! RerunDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	if exists("g:Vm_pc_sign_number")
		silent exe 'sign unplace ' . g:Vm_pc_sign_number
	endif
	echohl Error | echo 'No active DBX prompt.' | echohl None
	let cmd=["rerun"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! StopDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	unmap <2-LeftMouse>
	let g:ProgBroken = 0
	let g:dbx_running = 0
	exe 'sign unplace *' 
	let cmd=["quit"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! PrintDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["p " . expand("<cword>")]
	call writefile(cmd, "dbx_pipe")
endfun

fun! GotoDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	if exists("g:Vm_pc_sign_number")
		silent exe 'sign unplace ' . g:Vm_pc_sign_number
	endif
	let ln = line(".")
	let cmd=["goto " . ln]
	call writefile(cmd, "dbx_pipe")
endfun

fun! ClearAllDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	exe 'sign unplace *' 
	let cmd=["delall"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! BrkDBX()
	call system('pkill -INT -P `pgrep -u $USER dbx`')
	"echohl Type | echo 'Breaking the execution!' | echohl None
endfun

fun! LocateDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["sc"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! DumpDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let cmd=["dump"]
	call writefile(cmd, "dbx_pipe")
endfun

fun! SetPromptDBX()
	let g:ProgBroken = 1
	echo '(dbx)'
endfun

fun! SetNextStatementDBX()
	if (g:ProgBroken != 1)
		echohl Error | echo 'Cannot perform action. Program is running or debugger is not connected.' | echohl None
		return
	endif
	let g:ProgBroken = 0
	let ln = line(".")
	let cmd=["stop at " . ln . " -temp; cont at " . ln]
	call writefile(cmd, "dbx_pipe")
endfun

com! -nargs=*	D call writefile([<q-args>], "dbx_pipe")
com! SetPrompt call SetPromptDBX() 
vmap <silent> <F12> y:D print <C-R>"<CR>
vmap <silent> <F11> y:D examine &<C-R>" / 8<CR>
vmap <silent> <F10> y:D examine <C-R>" / 8<CR>
nmap <silent> <C-A-w> :D display <C-R>=expand("<cword>")<CR><CR>
vmap <silent> <C-A-w> y:D display <C-R>"<CR>
nmap <silent> <C-A-u> :D undisplay 0<CR>
nmap <silent> <C-p> :D print -L *<C-R>=expand("<cword>")<CR><CR>
vmap <silent> <C-p> y:D print -L *<C-R>"<CR>
" Assign value to variable
nmap <A-s> :D assign <C-R>=expand("<cword>")<CR> =
vmap <A-s> y:D assign <C-R>" =
